/*
 * 
 * Copyright 2011 visual-analysis-of-cancer.
 * This file is part of visual-analysis-of-cancer.
 * 
 * visual-analysis-of-cancer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * visual-analysis-of-cancer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * visual-analysis-of-cancer:
 * An tool to analye the WHO and GLOBOCAN cancer data.
 * http://code.google.com/p/visual-analysis-of-cancer/
 * 
 * 
 * 
 * Authors: Erik Sniegula, Johannes Dahlke
 * 
 * Module:  Programming in Processing for Visual Analytics
 * Group:   20
 * 
 * Created: Aug. 2011
 * Version: 1.0
 *
 */
 




package de.fuberlin.processing.who.cancer.controlp5ex;

import java.util.StringTokenizer;
import java.util.Vector;

import processing.core.PApplet;
import processing.core.PFont;
import controlP5.Button;
import controlP5.ControlEvent;
import controlP5.ControlListener;
import controlP5.ControlP5;
import controlP5.Controller;
import controlP5.ControllerDisplay;
import controlP5.ControllerGroup;
import controlP5.Label;
import controlP5.Tooltip;
import de.fuberlin.processing.who.cancer.gui.healthgraph.CancerDisc;
import de.fuberlin.processing.who.cancer.gui.healthgraph.TooltipPropertiedControllerInterface;
import de.fuberlin.processing.who.cancer.utils.Fonts;
import de.fuberlin.processing.who.cancer.utils.Test;


public class CustomTooltip extends Tooltip {


	public CustomTooltip() {
    super();
    this.setDisplay( new CustomTooltipDisplay());
	}
	
	public void register( TooltipPropertiedControllerInterface theController) {
		super.register( (Controller) theController, theController.getTooltipText());
	}
	
	public void showTooltip( Controller theController) {
		this.activate( theController);
	}
	
	public void updateTooltipController( TooltipPropertiedControllerInterface theController) {
	   	unregister( (Controller) theController);
	   	super.register( (Controller) theController, theController.getTooltipText());
	}

	public void clear() {
		map.clear();
	}
	
	public void clearAllInstancesOf( Class theClass) {
		Vector<Object> objectsToRemove = new Vector<Object>();
		for ( Object object : map.keySet()) {
			if ( theClass.isInstance( object)) {
				objectsToRemove.add( object);
			}
		}
		for ( Object object : objectsToRemove) {
			map.remove( object);
		}
		System.gc();
	}
	
	@Override
	public void activate( Controller theController) {
		// TODO Auto-generated method stub
    if ( theController instanceof CancerDisc) {
		  // fit label width
    	String tooltipText = map.get( theController);
    	if ( Test.isUnassigned( tooltipText))
    		return;
      float tooltipTextWidth = theController.getControlWindow().papplet().textWidth( tooltipText);
      getLabel().setWidth( (int) tooltipTextWidth);
      getLabel().position.y = 40;
    }
		super.activate( theController);
	}
	
	class CustomTooltipDisplay implements ControllerDisplay, ControlListener {

		
		public void display(PApplet theApplet, Controller theController) {
			PApplet p = theApplet;
			p.pushStyle(); 
		
			String text = getLabel().getText();
			int textHeight = getLabel().getHeight();
		  p.textFont( Fonts.getDejavuSans(), 10);
		  int textWidth = 20;
			// determine width of hint box
			StringTokenizer tokenizer = new StringTokenizer( text, "\n");
			String nextLine;
			int nextLineLen;
			while( tokenizer.hasMoreTokens()) {
				nextLine = tokenizer.nextToken();
				if ( ( nextLineLen = (int) p.textWidth( nextLine)) > textWidth)
					textWidth = nextLineLen;
		  }

			// wenn CancerDisc, dann Platz für Locationname in Fettdruck berücksichtigen
			if ( theController instanceof CancerDisc) {
				textHeight += 60;
				
				CancerDisc cancerDisc = (CancerDisc) theController;
				nextLine = cancerDisc.getLocationName();
				if ( ( nextLineLen = (int) p.textWidth( nextLine)) > textWidth)
					textWidth = nextLineLen;
			}
			// add a margin
			textWidth += 20;
			
			p.fill( getBackgroundColor(), getAlpha());
			p.rect(0, 0, textWidth + getBorder() * 2, textHeight + getBorder());
			p.triangle(3, 0, 7, -4, 11, 0);
			p.tint(255, PApplet.map(getAlpha(), 0, getMaxAlpha(), 0, 255));


			if ( theController instanceof CancerDisc) {
				CancerDisc cancerDisc = (CancerDisc) theController;
				p.textFont( Fonts.getArialBold(), 14);
				p.fill( p.color(  0xA));
				p.text( cancerDisc.getLocationName(), 10, 20);
				p.textFont( Fonts.getDejavuSans(), 10);
				p.text( getLabel().getText(), 10, 50);  
			} else {	
				p.fill( p.color(  0xA));
				p.text( getLabel().getText(), 10, 12); 
			}
			
			p.tint(255);
			p.popStyle();
		}

		@Override
		public void controlEvent( ControlEvent theEvent) {
		}
	}
	
}
